<?php

namespace app\api\model;

use think\Model;


class Task extends Model
{
    // 表名
    protected $name = 'task';


    //批量写入任务、
    public static function addTask($exams_list_id)
    {

        $blockList = db('exam_paper_block')->where('exams_list_id', $exams_list_id)->select();
        if(empty($blockList)) return;
        $paperImage = db('exam_paper_images')->where(['exams_list_id' => $exams_list_id, 'page' => 1])->where('status', 'in', [2, 4])->select();
        if(empty($paperImage)) return;
        foreach ($blockList as $key => $value) {
            
            foreach ($paperImage as $paper) {
                $key = 'makeBlock' . $value['id'];
                $checkTask = db('task')->where(['exams_list_id'=>$paper['exams_list_id'],'student_id'=>$paper['student_id'],'block_id'=>$value['id']])->find();
                switch ($value['type']) {
                    case 'alone':
                        if(!$checkTask){
                            db('task')->insert([
                                'exams_id' => $paper['exams_id'],
                                'exams_list_id' => $paper['exams_list_id'],
                                'student_id' => $paper['student_id'],
                                'school_id' => $paper['school_id'],
                                'block_id' => $value['id'],
                                'key' =>  $key,
                                'status' => 0,
                                'blockType' => $value['type'],
                                'num' =>  1,
                            ]);
                        }
                        break;
                    case 'dual':
                        for ($i = 1; $i <= 2; $i++) {
                            if(!$checkTask){
                                db('task')->insert([
                                    'exams_id' => $paper['exams_id'],
                                    'exams_list_id' => $paper['exams_list_id'],
                                    'student_id' => $paper['student_id'],
                                    'school_id' => $paper['school_id'],
                                    'block_id' => $value['id'],
                                    'key' =>  $key,
                                    'status' => 0,
                                    'blockType' => $value['type'],
                                    'num' =>  $i + 1,
                                ]);
                            }
                            
                        }
                        break;
                }
            }
        }
        return true;
    }

    //单个写入任务、
    public static function addOneTask($exams_list_id, $paperId)
    {

        $blockList = db('exam_paper_block')->where('exams_list_id', $exams_list_id)->select();
        if(empty($blockList)) return;
        $paper = db('exam_paper_images')->where(['id' => $paperId, 'page' => 1])->find();
        if(empty($paper)) return;
        foreach ($blockList as $key => $value) {
            $key = 'makeBlock' . $value['id'];
            $checkTask = db('task')->where(['exams_list_id'=>$paper['exams_list_id'],'student_id'=>$paper['student_id'],'block_id'=>$value['id']])->find();
            switch ($value['type']) {
                case 'alone':
                    if(!$checkTask){
                        db('task')->insert([
                            'exams_id' => $paper['exams_id'],
                            'exams_list_id' => $paper['exams_list_id'],
                            'student_id' => $paper['student_id'],
                            'school_id' => $paper['school_id'],
                            'block_id' => $value['id'],
                            'key' =>  $key,
                            'status' => 0,
                            'blockType' => $value['type'],
                            'num' =>  1,
                        ]);
                    }
                    break;
                case 'dual':
                    for ($i = 1; $i <= 2; $i++) {
                        if(!$checkTask){
                            db('task')->insert([
                                'exams_id' => $paper['exams_id'],
                                'exams_list_id' => $paper['exams_list_id'],
                                'student_id' => $paper['student_id'],
                                'school_id' => $paper['school_id'],
                                'block_id' => $value['id'],
                                'key' =>  $key,
                                'status' => 0,
                                'blockType' => $value['type'],
                                'num' =>  $i + 1,
                            ]);
                        }
                    }
                    break;
            }
        }
        return true;
    }

    //获取任务
    public static function getTasks($type, $exams_list_id, $block_id, $teacher_id)
    {
        $key = 'makeBlock' . $block_id;
        switch ($type) {
            case '1':
                #单评
                $info = self::where(['exams_list_id' => $exams_list_id, 'block_id' => $block_id, 'blockType' => 'alone', 'status' => 1, 'num' => 1])->where("FIND_IN_SET(" . $teacher_id . ",teacher_ids)")->find();
                if ($info) {
                    return $info;
                } else {
                    $data = self::where(['exams_list_id' => $exams_list_id, 'block_id' => $block_id, 'blockType' => 'alone', 'status' => 0, 'num' => 1])->order('id asc')->lock(true)->find();
                    if ($data) {
                        self::where('id', $data['id'])->update(['status' => 1, 'teacher_ids' => $teacher_id]);
                    }
                    return $data;
                }
                break;
            case '2':
                #初评
                $info = self::where(['exams_list_id' => $exams_list_id, 'block_id' => $block_id, 'blockType' => 'dual', 'status' => 1, 'num' => 2])->where("FIND_IN_SET(" . $teacher_id . ",teacher_ids)")->find();
                if ($info) {
                    return $info;
                } else {
                    $data = self::where(['exams_list_id' => $exams_list_id, 'block_id' => $block_id, 'blockType' => 'dual', 'status' => 0, 'num' => 2])->order('id asc')->lock(true)->find();
                    if ($data) {
                        self::where('id', $data['id'])->update(['status' => 1, 'teacher_ids' => $teacher_id]);
                    }
                    return $data;
                }
                break;
            case '3':
                #终评
                $info = self::where(['exams_list_id' => $exams_list_id, 'block_id' => $block_id, 'blockType' => 'dual', 'status' => 1, 'num' => 3])->where("FIND_IN_SET(" . $teacher_id . ",teacher_ids)")->find();
                if ($info) {
                    return $info;
                } else {
                    $data = self::where(['exams_list_id' => $exams_list_id, 'block_id' => $block_id, 'blockType' => 'dual', 'status' => 0, 'num' => 3])->order('id asc')->lock(true)->find();
                    if ($data) {
                        self::where('id', $data['id'])->update(['status' => 1, 'teacher_ids' => $teacher_id]);
                    }
                    return $data;
                }
                break;
        }
    }
}
